package com.si.anddos.tests;

import java.net.URL;

import com.si.anddos.dpadapters.Account;
import com.si.anddos.library.main.AccountDetail;
import com.si.anddos.library.main.factory.ServiceProviderFactory;
import com.si.anddos.library.provider.ServiceProvider;
import com.si.anddos.library.provider.ServiceProviderAdapter;
import com.si.anddos.library.provider.ServiceProviderListener;

/**
 * 
 * @author Carlos Martinez
 */

public class ServiceProviderTest extends ServiceProviderAdapter {

	public static void main(String[] args) {
		
		try {
			
			URL url = new URL("http://rapidshare.com/files/428730419/P.gif");
			
			//ServiceProvider serviceProvider = ServiceProviderFactory.getProvider(url);
//			serviceProvider.setServiceProviderListener(new ServiceProviderTest());
//			serviceProvider.setFileSaver(new FileSaver(new File(System.getProperty("user.home"), serviceProvider.getFileInfo().getName())));

			
			
			Account account = new Account();

			account.setUserName("anddos5");
			account.setPassword("anddos3poi0");
			account.setService("rapidshare.com");
			
			
			ServiceProvider serviceProvider = ServiceProviderFactory.getProvider(account);
			serviceProvider.download(0);
			
			AccountDetail accountDetail = serviceProvider.getAccountDetail(account);
			
			if (accountDetail != null) {
				System.out.println(accountDetail.getEmail());
				System.out.println(accountDetail.getCurrentSpace());
				System.out.println(accountDetail.getBilledUntil());
			}
			
//			serviceProvider.download(account);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void downloadStart(ServiceProvider provider) {

		System.out.println("Download did start: " + provider.getURL());
	}

	@Override
	public void downloadWait(ServiceProvider provider, int time) {
		
		System.out.println("Download will wait for " + time + " seconds.");
	}

	@Override
	public void downloadProgress(ServiceProvider provider, long size, long downloaded) {
		
		if (size != 0) {
			System.out.println("Downloaded " + ((100 * downloaded ) / size) + "%.");
		}
	}

	@Override
	public void downloadFinish(ServiceProvider provider) {
		
		System.out.println("Download did finish");
	}

	@Override
	public void downloadFail(ServiceProvider provider, Throwable throwable) {
		
		System.out.println("Download did fail with error: " + throwable);
		
		throwable.printStackTrace();
	}

	@Override
	public void downloadStop(ServiceProvider provider) {

		System.out.println("Download did stop");
	}
}
